package cn.edu.scau.Utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SaveVideoUtils {

	// 是否转换成为flvfile
	public static boolean isConvertFLV(String file) {
		boolean result = false;
		String ext = file.substring(file.lastIndexOf(".") + 1, file.length()).toLowerCase();
		if (ext.equals("avi")) {
			result = true;
		} else if (ext.equals("mpg")) {
			result = true;
		} else if (ext.equals("wmv")) {
			result = true;
		} else if (ext.equals("3gp")) {
			result = true;
		} else if (ext.equals("mov")) {
			result = true;
		} else if (ext.equals("mp4")) {
			result = true;
		} else if (ext.equals("asf")) {
			result = true;
		} else if (ext.equals("asx")) {
			result = true;
		} else if (ext.equals("flv")) {
			result = true;
		}
		return result;
	}

	// 是否转换成为avifile
	public static boolean isConvertAVI(String file) {
		boolean result = false;
		String ext = file.substring(file.lastIndexOf(".") + 1, file.length()).toLowerCase();
		if (ext.equals("wmv9")) {
			result = true;
		} else if (ext.equals("rm")) {
			result = true;
		} else if (ext.equals("rmvb")) {
			result = true;
		}
		return result;
	}

	// 进行转换
	public static boolean executeCodecs(String ffmpegDir, String tempPath, String srcFilePath, String codcFilePath,
			String mediaPicPath) {
		// mencoder的路径
		String mencoderPath = ffmpegDir + "mencoder.exe";
		// ffmpeg的路径
		String ffmpegPath = ffmpegDir + "ffmpeg.exe";
		boolean mark = true;
		// 临时保存路径
		tempPath +=  String.valueOf(System.currentTimeMillis()) + ".avi";
		// 转化为avi
		if (isConvertAVI(srcFilePath)) {
			System.out.println("先转换成为avi");
			mark = convertAVI(mencoderPath, srcFilePath, tempPath);
			srcFilePath = tempPath;
		}
		// 转换为flv
		if (isConvertFLV(srcFilePath) && mark) {
			System.out.println("转换为flv");
			mark = convertFLV(ffmpegPath, srcFilePath, codcFilePath);
			mark = cutPic(ffmpegPath, srcFilePath, mediaPicPath);
		} else {
			System.out.println("视频格式不正确");
			mark = false;
		}
		// 删除avi格式的视频
		deleteAVIFile(tempPath);
		return mark;
	}

	// 转换成flv格式
	private static boolean convertFLV(String ffmpegPath, String srcFilePath, String codcFilePath) {
		File file = new File(ffmpegPath);
		File srcFile = new File(srcFilePath);
		if (file.exists()) {
			System.out.println("ffmeg读取成功");
		}
		if (srcFile.exists()) {
			System.out.println("视频读取成功");
		}
		File codcFile = new File(codcFilePath);
		if(!codcFile.exists()){
			codcFile.mkdirs();
		}
		List<String> convert = new ArrayList<String>();
		convert.add(ffmpegPath);
		convert.add("-i");
		convert.add(srcFilePath);
		convert.add("-ab");
		convert.add("128");
		convert.add("-ac");
		convert.add("2");
		convert.add("-qscale");
		convert.add("6");
		convert.add("-ar");
		convert.add("22050");
		convert.add("-r");
		convert.add("29.97");
		convert.add("-b");
		convert.add("5942.13");
		convert.add("-s");
		convert.add("1280x720");
		convert.add("-y");
		convert.add(codcFilePath);

		boolean mark = true;
		try {
			Process proc = new ProcessBuilder(convert).redirectErrorStream(true).start();
			BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
			String line;
			while ((line = stdout.readLine()) != null) {
				System.out.println(line);
			}
		} catch (Exception e) {
			mark = false;
			System.out.println(e);
			e.printStackTrace();
		}
		return mark;
	}

	// 截图
	private static Boolean cutPic(String ffmpegPath, String srcFilePath, String mediaPicPath) {
		File mediaPic = new File(mediaPicPath);
		if(!mediaPic.exists()){
			mediaPic.mkdirs();
		}
		List<String> cutpic = new ArrayList<String>();
		cutpic.add(ffmpegPath);
		cutpic.add("-i");
		cutpic.add(srcFilePath);
		cutpic.add("-y");
		cutpic.add("-f");
		cutpic.add("image2");
		cutpic.add("-ss");
		cutpic.add("7");
		cutpic.add("-t");
		cutpic.add("0.001");
		cutpic.add("-s");
		cutpic.add("800*280");
		cutpic.add(mediaPicPath);

		boolean mark = true;
		ProcessBuilder builder = new ProcessBuilder();
		try {
			builder.command(cutpic);
			builder.redirectErrorStream(true);
			builder.start();
		} catch (Exception e) {
			mark = false;
			System.out.println(e);
			e.printStackTrace();
		}
		return mark;
	}

	// 转换成为avi格式
	private static boolean convertAVI(String mencoderPath, String srcFilePath, String codcFilePath) {
		List<String> commend = new ArrayList<String>();
		commend.add(mencoderPath);
		commend.add(srcFilePath);
		commend.add("-oac");
		commend.add("lavc");
		commend.add("-lavcopts");
		commend.add("acodec=mp3:abitrate=64");
		commend.add("-ovc");
		commend.add("xvid");
		commend.add("-xvidencopts");
		commend.add("bitrate=600");
		commend.add("-of");
		commend.add("avi");
		commend.add("-o");
		commend.add(codcFilePath);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commend);
			builder.redirectErrorStream(true);
			Process p = builder.start();
			String line = null;
			BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
			while ((line = stdout.readLine()) != null) {
				System.out.println(line);
			}
			p.waitFor();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 删除avi文件
	public static void deleteAVIFile(String tempFile) {
		File file = new File(tempFile);
		if (file.exists()) {
			file.delete();
		}
	}
}